(function($) {
	addEvent(window,'load',init);
	function addEvent(obj, type, fn)
	{
		if (obj.attachEvent)
		{
			obj['e'+type+fn] = fn;
			obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
			obj.attachEvent('on'+type, obj[type+fn]);
		}
		else
			obj.addEventListener(type, fn, false);
	}
	function FireEvent(elem, eventName)
	{
		if (document.all)
		{
			elem.fireEvent(eventName);
		}
		else
		{
			 var evt = document.createEvent('HTMLEvents');
			 evt.initEvent('change',true,true);
			 elem.dispatchEvent(evt);
		}
	}
	function removeEvent(obj, type, fn)
	{
		if (obj.detachEvent)
		{
			obj.detachEvent('on'+type, obj[type+fn]);
			obj[type+fn] = null;
		}
		else
		{
			obj.removeEventListener(type, fn, false);
		}
	}
	$.fn.extend({
		tyFormCheck: function() {
			var f = $(this).parentsUntil("form").parent().get(0);
			var flag = true;
			for(j = 0; j < f.elements.length; j++)
            {
                var e = f.elements[j];
                if(e.type == "text" || e.type == "password"){
                	$(e).blur(validateOnChange(e,false));
                	if(flag && $(e).next().hasClass("ajax_false")){
                		flag = false;
                	}
                }
			}
			return flag;
		}
	});
	function init(){
		for(var i = 0; i < document.forms.length; i++)
        {
            var f = document.forms[i];
            var needsValidation = false;
            for(j = 0; j < f.elements.length; j++)
            {
                var e = f.elements[j];
                if(e.type != "text" && e.type!="password" && e.type!='select-one' && e.type!='textarea') continue;
                var pattern = e.getAttribute("pattern");
                var modern = e.getAttribute("modern");
                var onfocustip = e.getAttribute("onfocustip");
                if(onfocustip == null || onfocustip == ""){
                	onfocustip = $(e).next("").html();
                	$(e).next().attr("onfocustip",onfocustip);
                }
                $(e).blur(function(){
                	validateOnChange(this);
                });
					addEvent(e,'focus',validateOnFocus);
                    needsValidation = true;
            }
        }
	}
	function validateOnFocus(){
		var textfield = this;
		var tips= $(textfield).next().attr("onfocustip");
		$(this).next().removeClass('ajax_true').removeClass('ajax_false');
		$(this).next().css("color","gray");
		$(this).next().html(tips);
	}
})(jQuery);

function validateOnChange(e){
	var async = (typeof(arguments[1]) == "undefined") ? false : true;
	var textfield = e;
	var pattern = textfield.getAttribute("pattern");
	var validate_url = textfield.getAttribute("validate_url");
	var modern = textfield.getAttribute("modern");
	var tips = textfield.getAttribute("tips");
	switch (modern) {
	case 'required':
		pattern = /\S+/i;
		break;
	case 'username':
		pattern = /^\w{6,15}$/i;
		break;
	case 'password':
		pattern = /^^\w{9,16}$/i;
		break;
	case 'repassword':
		pattern = /^^\w{9,16}$/i;
		break;
	case 'email':
		pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)+$/i;
		break;
	case 'qq':
		pattern = /^[1-9][0-9]{4,}$/i;
		break;
	case 'id':
		pattern = /^\d{15}(\d{2}[0-9x])?$/i;
		break;
	case 'ip':
		pattern = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/i;
		break;
	case 'zip':
		pattern = /^\d{6}$/i;
		break;
	case 'mobi':
		pattern = /^[0-9]{8,20}$/;
		break;
	case 'phone':
		pattern = /^((\d{3,4})|\d{3,4}-)?\d{7,8}(-\d+)*$/i;
		break;
	case 'url':
		pattern = /^[a-zA-z]+:\/\/(\w+(-\w+)*)(\.(\w+(-\w+)*))+(\/?\S*)?$/i;
		break;
	case 'date':
		pattern = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/i;
		break;
	case 'datetime':
		pattern = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29) (?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9])$/i;
		break;
	case 'int':
		pattern = /^\d+$/i;
		break;
	case 'float':
		pattern = /^\d+\.?\d*$/i;
		break;
	default:
		pattern = pattern;
	}
	var value = $(e).val();
	var name = $(e).attr("name");
	if((value!='' && value.search(pattern) == -1) || (value == '')){
		$(e).next().css("color","red");
		$(e).next().addClass('ajax_false')
		$(e).next().html(tips);
	}
	else{
		for(var i=0; i<formJson.length; i++)  
		  {  
		     if(name == formJson[i].inputName){
		    	 var jsondata = $("input[name='"+formJson[i].inputName+"']").serialize();
					$.ajax({url: formJson[i].ajaxUrl, type: "GET", dataType: "JSON", data: jsondata, async: async,
		                success: function(result) {
		                	if(result.status){
		                		$("input[name='"+formJson[i].inputName+"']").next().removeClass('ajax_true').removeClass('ajax_false');
		                		$("input[name='"+formJson[i].inputName+"']").next().addClass('ajax_true');
		                		$("input[name='"+formJson[i].inputName+"']").next().html("");
		                	}else{
		                		$("input[name='"+formJson[i].inputName+"']").next().removeClass('ajax_true').removeClass('ajax_false');
		                		$("input[name='"+formJson[i].inputName+"']").next().addClass('ajax_false');
		                		$("input[name='"+formJson[i].inputName+"']").next().css("color","red");
		                		$("input[name='"+formJson[i].inputName+"']").next().html(result.info);
		                	}
		                }});
					return false;
		     }
		  }
		$(e).next().addClass('ajax_true')
		$(e).next().html("");
		if(modern == "password"){
			var repassword = $(e).parentsUntil("table").find("input[name='repassword']").val();
			var password = $(e).val();
			if(password === repassword){
				$(e).next().addClass('ajax_true');
				$(e).next().html("");
				$(e).parentsUntil("table").find("input[name='repassword']").next().removeClass('ajax_false');
				$(e).parentsUntil("table").find("input[name='repassword']").next().addClass('ajax_true');
				$(e).parentsUntil("table").find("input[name='repassword']").next().html("");
			}else{
				$(e).parentsUntil("table").find("input[name='repassword']").next().addClass('ajax_false');
				$(e).parentsUntil("table").find("input[name='repassword']").next().html("两次输入的密码不一致");
				$(e).parentsUntil("table").find("input[name='repassword']").next().css("color","red");
			}
		}
		else if(modern == "repassword"){
			var password = $(e).parentsUntil("table").find("input[name='password']").val();
			var repassword = $(e).val();
			if(password === repassword){
				$(e).next().addClass('ajax_true');
				$(e).next().html("");
			}else{
				$(e).next().css("color","red");
				$(e).next().addClass('ajax_false');
				$(e).next().html("两次输入的密码不一致");
			}
		}
	}
}